using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Openmerch.Change;
using JlPay.Sdk.Models.Openmerch.Change;

namespace JlPay.Sdk.Examples.Openmerch.Change
{
    /// <summary>
    /// 开放商户 - 结算账户变更功能使用示例
    /// </summary>
    public class SettleAccountChangeDemo
    {
        /// <summary>
        /// 构建结算账户变更请求
        /// </summary>
        private static SettleAccountChangeRequest BuildSettleAccountChangeRequest()
        {
            // 构建辅助证明材料
            var proofInfo = new List<Dictionary<string, object>>
            {
                new Dictionary<string, object>
                {
                    ["proof_type"] = "01",  // 证明类型：01-营业执照
                    ["proof_name"] = "营业执照",
                    ["proof_url"] = "https://example.com/license.jpg"
                },
                new Dictionary<string, object>
                {
                    ["proof_type"] = "02",  // 证明类型：02-身份证
                    ["proof_name"] = "法人身份证",
                    ["proof_url"] = "https://example.com/idcard.jpg"
                }
            };

            var request = new SettleAccountChangeRequest
            {
                ApplyId = "b8659ea518d14c99b78b5ad000a0803b",  // 使用新的申请单号
                MerchNo = "849584358140109",  // 使用新的商户号
                SettleType = "01",  // 结算类型：01-对私
                AccountName = "张三",
                AccountNo = "6222021234567890123",
                BankCode = "ICBC",  // 工商银行
                BankBranchId = "102100000000",
                BankBranchName = "工商银行北京朝阳支行",
                AccountPic = "https://example.com/account.jpg",
                BankSetPhone = "13800138000",
                CertType = "01",  // 证件类型：01-身份证
                DelegateeIdCard = "110101199001011234",
                DelegateeCertPeriodBegin = "2020-01-01",
                DelegateeCertPeriodEnd = "2030-01-01",
                AcctLicense = "https://example.com/license.jpg",
                AcctCertFrontPic = "https://example.com/idcard_front.jpg",
                AcctCertBackPic = "https://example.com/idcard_back.jpg",
                AcctAuthLetter = "https://example.com/auth_letter.jpg",
                IdentityFacePic = "https://example.com/face.jpg",
                AcctFacePic = "https://example.com/acct_face.jpg",
                ProofInfo = proofInfo
            };

            return request;
        }

        /// <summary>
        /// 结算账户变更示例
        /// </summary>
        public static async Task SettleAccountChangeDemoAsync()
        {
            Console.WriteLine("开始执行结算账户变更示例");

            // 使用DemoConfig创建配置对象
            var config = DemoConfig.CreateConfigWithCrypto();
            Console.WriteLine("配置初始化完成，已启用自动加密和解密功能");

            var changeService = new ChangeService(config);

            // 创建结算账户变更请求
            var request = BuildSettleAccountChangeRequest();

            try
            {
                // 发送请求
                var response = await changeService.ChangeSettleAccountAsync(request);

                // 使用优化的响应处理
                response.HandleResponse("结算账户变更", config);
            }
            catch (Exception ex)
            {
                // 使用优化的异常处理
                var response = new SettleAccountChangeResponse();
                response.HandleResponseWithException("结算账户变更", config, ex);
            }

            Console.WriteLine("结算账户变更示例执行完毕");
        }

        /// <summary>
        /// 主程序入口
        /// </summary>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 结算账户变更示例 开始 =====");
            Console.WriteLine("=" + new string('=', 50));

            await SettleAccountChangeDemoAsync();

            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 结算账户变更示例执行完毕 =====");
            Console.WriteLine("=" + new string('=', 50));
        }
    }
} 